﻿var calendarContainer = $('#calendarcontainer');

var RegisterContentEvents = function () {

    ActivatePopover($("[rel=popover]"));

    $('.pagelink').click(function () {
        window.location = $(this).attr("url");
    });
};

var SetNewMonth = function () {
    var month = $('#MonthText').val();
    $("#month").attr("value", month);
    $("#month").html(month + "<b class=\"caret\"></b>");

    var year = $('#YearText').val();
    $("#year").attr("value", year);
    $("#year").html(year + "<b class=\"caret\"></b>");
};

var CurrentMonthData = function() {
    return { Month: $('#month').attr('value'), Year: $('#year').attr('value') };
};

var LoadMonth = function () {
    $(calendarContainer).fadeOut(500);
    PostReturningHtml('/schedule/month', CurrentMonthData(), calendarContainer, function () {
        RegisterContentEvents();
    });
};

var RegisterEvents = function () {
    $('.selectablemonth').click(function () {
        $('#MonthText').val($(this).text());
        SetNewMonth();
        LoadMonth();
    });

    $('.selectableyear').click(function () {
        $('#YearText').val($(this).text());
        SetNewMonth();
        LoadMonth();
    });

    $('#next').click(function () {
        $(calendarContainer).fadeOut(500);
        PostReturningHtml('/schedule/nextmonth', CurrentMonthData(), calendarContainer, function () {
            SetNewMonth();
            RegisterContentEvents();
        });
    });

    $('#previous').click(function () {
        $(calendarContainer).fadeOut(500);
        PostReturningHtml('/schedule/previousmonth', CurrentMonthData(), calendarContainer, function () {
            SetNewMonth();
            RegisterContentEvents();
        });
    });
};

$(document).ready(function() {
    RegisterEvents();
    LoadMonth();
});